﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using JelloPhysics;
using System.Diagnostics;
using Microsoft.Xna.Framework;

namespace Demo
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            CompositionTarget.Rendering += new EventHandler(this.Storyboard_Completed);
        }

        private Brush borderBrush = new SolidColorBrush(Colors.Black);
        private Random rand = new Random();
        private List<Body> staticBodies = new List<Body>();
        private World world = new World();

        private void CreatePlatforms()
        {
            foreach (Body body in this.staticBodies)
            {
                this.world.removeBody(body);
                this.LayoutRoot.Children.Remove(body.ObjectTag as Shape);
            }
            double num = this.LayoutRoot.ActualHeight / 10.0;
            double num2 = num / 2.0;
            for (int i = 0; i < 10; i++)
            {
                float x = (float) (3.0 + (this.rand.NextDouble() * 10.0));
                ClosedShape shape = new ClosedShape();
                shape.begin();
                shape.addVertex(new Vector2(-x, -1f));
                shape.addVertex(new Vector2(-x, 1f));
                shape.addVertex(new Vector2(x, 1f));
                shape.addVertex(new Vector2(x, -1f));
                shape.finish();
                Body item = new Body(this.world, shape, float.PositiveInfinity, new Vector2((float) ((this.rand.NextDouble() * num) - num2), (float) ((((this.rand.NextDouble() * num) * 0.75) - num2) - 5.0)), (float) (((this.rand.NextDouble() * 3.1415926535897931) / 8.0) - 0.19634954084936207), Vector2.One, false);
                this.staticBodies.Add(item);
            }
        }

        private void DropBalloon(object sender, RoutedEventArgs e)
        {
            ClosedShape s = new ClosedShape();
            s.begin();
            for (double i = 0.0; i < 6.2831853071795862; i += 0.31415926535897931)
            {
                s.addVertex(new Vector2((float) Math.Sin(i), (float) Math.Cos(i)));
            }
            s.finish();
            double num2 = this.LayoutRoot.ActualHeight / 50.0;
            float num3 = (float) (1.0 + (2.0 * this.rand.NextDouble()));
            FallingBody body = new FallingBody(this.world, s, 1f, 40f, 100f, 10f, 100f, 10f, new Vector2((float) ((this.rand.NextDouble() * num2) - (num2 / 2.0)), 20f), MathHelper.ToRadians((float) new Random().Next(360)), new Vector2(num3));
            int count = s.Vertices.Count;
            for (int j = 0; j < count; j++)
            {
                body.addInternalSpring(j, (j + 4) % count, 400f, 12f);
            }
        }

        private void MovePlatforms(object sender, RoutedEventArgs e)
        {
            this.CreatePlatforms();
        }

        private void Storyboard_Completed(object sender, EventArgs e)
        {
            double actualWidth = this.LayoutRoot.ActualWidth;
            double actualHeight = this.LayoutRoot.ActualHeight;
            if ((actualWidth > 0.0) && (actualHeight > 0.0))
            {
                if (this.staticBodies.Count < 1)
                {
                    this.CreatePlatforms();
                }
                for (int i = 0; i < 6; i++)
                {
                    this.world.update(0.008333334f);
                }
                List<Body> list = new List<Body>();
                foreach (Body body in this.world)
                {
                    Polygon objectTag = body.ObjectTag as Polygon;
                    if (objectTag == null)
                    {
                        objectTag = new Polygon();
                        objectTag.Fill = new SolidColorBrush(Color.FromArgb(50, (byte) this.rand.Next(0xff), (byte) this.rand.Next(0xff), (byte) this.rand.Next(0xff)));
                        objectTag.StrokeThickness = 4.0;
                        objectTag.Stroke = this.borderBrush;
                        this.LayoutRoot.Children.Add(objectTag);
                        body.ObjectTag = objectTag;
                    }
                    PointCollection points = new PointCollection();
                    bool flag = false;
                    double num4 = actualHeight / 50.0;
                    foreach (PointMass mass in body)
                    {
                        double x = (actualWidth / 2.0) + (mass.Position.X * num4);
                        double y = (actualHeight / 2.0) - (mass.Position.Y * num4);
                        if (((x >= 0.0) && (x < actualWidth)) && ((y >= 0.0) && (y < actualHeight)))
                        {
                            flag = true;
                        }
                        points.Add(new Point(x, y));
                    }
                    objectTag.Points = points;
                    if (!flag)
                    {
                        list.Add(body);
                    }
                }
                foreach (Body body2 in list)
                {
                    this.world.removeBody(body2);
                    this.LayoutRoot.Children.Remove(body2.ObjectTag as Shape);
                }
            }
        }
    }
}
